package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.bean.ShoppingCart;
import com.itheima.dao.ShoppingCartDao;
import com.itheima.service.ShoppingCartService;
import com.itheima.utils.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service
@Slf4j
@Transactional
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartDao shoppingCartDao;

    /**
     * 查询该用户下的购物车商品
     *
     * @return
     */
    @Override
    public List<ShoppingCart> selectByUserId() {
        Long userId = BaseContext.getCurrentId();
        System.out.println(userId);
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId, userId);
        List<ShoppingCart> shoppingCarts = shoppingCartDao.selectList(qw);
        return shoppingCarts;
    }

    /**
     * 往当前用户的购物车添加商品
     *
     * @param shoppingCart
     * @return
     */
    @Override
    public int insert(ShoppingCart shoppingCart) {
        Long currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(shoppingCart.getDishId()!=null,ShoppingCart::getDishId,shoppingCart.getDishId());
        qw.eq(shoppingCart.getSetmealId()!=null,ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        qw.eq(ShoppingCart::getUserId, currentId);
        ShoppingCart shoppingCarts = shoppingCartDao.selectOne(qw);
        if (shoppingCarts != null) {
            Integer number = shoppingCarts.getNumber();
            number++;
            shoppingCart.setNumber(number);
            shoppingCartDao.update(shoppingCart, qw);

            return 1;
        }

        shoppingCart.setUserId(currentId);
        int insert = shoppingCartDao.insert(shoppingCart);
        return insert;
    }

    /**
     * 清空当前用户的购物车
     *
     * @return
     */
    @Override
    public int delete() {
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId, userId);
        int row = shoppingCartDao.delete(qw);
        return row;
    }

    @Override
    public int sub(ShoppingCart shoppingCart) {
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId, userId);
        qw.eq(shoppingCart.getDishId()!=null,ShoppingCart::getDishId,shoppingCart.getDishId());
        qw.eq(shoppingCart.getSetmealId()!=null,ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        ShoppingCart shoppingCart1 = shoppingCartDao.selectOne(qw);
        if (shoppingCart1 != null) {
            if (shoppingCart1.getNumber() > 1) {
                Integer number = shoppingCart1.getNumber();
                number--;
                shoppingCart1.setNumber(number);
                shoppingCartDao.update(shoppingCart1,qw);
                return 1;
            }
            return shoppingCartDao.delete(qw);
        }
        return 0;
    }
}
